package com.android.views.date_select;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.navigation.Navigation;

import com.android.views.BaseFragment;
import com.android.views.R;
import com.android.views.databinding.FragmentDateSelectBinding;
import com.android.views.recyclerview.MyRecyclerViewCAdatper;
import com.yds.views.YDSDateSelectDialog;
import com.yds.views.YDSTopBar;

import org.jetbrains.annotations.NotNull;

import java.util.List;

@SuppressWarnings("all")
public class DateSelectFragment extends BaseFragment {
    private FragmentDateSelectBinding binding;

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private MyRecyclerViewCAdatper mAdapter;
    private List<String> mList;
    private int count = 10;

    private Handler mHandler = new Handler();

    public DateSelectFragment() {
    }

    public static DateSelectFragment newInstance(String param1, String param2) {
        DateSelectFragment fragment = new DateSelectFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_date_select, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding.setLifecycleOwner(this);

        binding.mYDSTopBar.setOnTopBarClickListener(new YDSTopBar.OnTopBarClickListener() {
            @Override
            public void onImageViewLeftBackClick() {
                Navigation.findNavController(binding.getRoot()).popBackStack();
            }

            @Override
            public void onTextViewLeftBackClick() {
            }

            @Override
            public void onTextViewRightMoreClick() {
            }

            @Override
            public void onImageViewRightMoreClick() {
            }

            @Override
            public void onImageViewRightMore2Click() {
            }
        });


        binding.mButtonSingle.setOnClickListener(v -> {
            selectDate(true);
        });

        binding.mButtonDouble.setOnClickListener(v -> {
            selectDate(false);
        });

    }

    private void selectDate(boolean isSingle) {
        YDSDateSelectDialog YDSDateSelectDialog = new YDSDateSelectDialog(getActivity());
        YDSDateSelectDialog.setSelectDayType(isSingle);
        YDSDateSelectDialog.setOnSelectDateListener(new YDSDateSelectDialog.OnSelectDateListener() {
            @Override
            public void selectDate(String startDate, String endDate) {
                if (TextUtils.isEmpty(startDate) && TextUtils.isEmpty(endDate)) return;
                Toast.makeText(getActivity(), startDate + " - " + endDate, Toast.LENGTH_SHORT).show();
            }
        });

        YDSDateSelectDialog.show();
    }

}